package com.scheduler.backend.controller;

import com.scheduler.backend.bl.ERPService;
import com.scheduler.backend.vo.ResponseVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.Date;

@RestController()
@CrossOrigin
@Tag(name = "ERP相关接口")
public class ERPController {

    @Autowired
    private ERPService erpService;

    @RequestMapping(value = "/getDeviceGanttByDate/{date}", method = RequestMethod.GET)
    @Operation(summary = "根据Date获取设备甘特图", description = "根据Date获取设备甘特图", method = "GET")
    @Parameter(name = "date", description = "日期", required = true)
    public ResponseVO getDeviceGanttByDate(@PathVariable("date") @DateTimeFormat(pattern = "yyyy-MM-dd") Date date,
                                           HttpServletResponse response) {
        if (response != null) {
            response.setHeader("Access-Control-Allow-Origin", "*");
            response.setHeader("Cache-Control", "no-cache");
        }
        return erpService.getDeviceGanttByDate(date);
//        List<DeviceGanttInfo> result = new ArrayList<>();
//        DeviceGanttInfo device1 = DeviceGanttInfo.builder()
//                .resourceName("线体")
//                .resourceID(1)
//                .agenda(Arrays.asList(new WorkForOrder("1234","12345", new Date(1605187300), new Date(1605187300))))
//                .build();
//        DeviceGanttInfo device2 = DeviceGanttInfo.builder()
//                .resourceName("线体")
//                .resourceID(1)
//                .agenda(Arrays.asList(new WorkForOrder("1234","12345", new Date(1605187300), new Date(1605187300))))
//                .build();
//        DeviceGanttInfo device3 = DeviceGanttInfo.builder()
//                .resourceName("线体")
//                .resourceID(1)
//                .agenda(Arrays.asList(new WorkForOrder("1234","12345", new Date(1605187300), new Date(1605187300))))
//                .build();
//        DeviceGanttInfo device4 = DeviceGanttInfo.builder()
//                .resourceName("线体")
//                .resourceID(1)
//                .agenda(Arrays.asList(new WorkForOrder("1234","12345", new Date(1605187300), new Date(1605187300))))
//                .build();
//        result.add(device1);
//        result.add(device2);
//        result.add(device3);
//        result.add(device4);
//        return ResponseVO.buildSuccess(result);
    }

    //按hour的暂时放着
    @RequestMapping(value = "/getDeviceLoadRatioByDateInHour/{date}", method = RequestMethod.GET)
    @Operation(summary = "根据Date获取设备负载率/小时", description = "根据Date获取设备负载率/小时", method = "GET")
    @Parameter(name = "date", description = "日期", required = true)
    public ResponseVO getDeviceLoadRatioByDateInHour(@PathVariable("date") @DateTimeFormat(pattern = "yyyy-MM-dd") Date date,
                                                     HttpServletResponse response) {
        if (response != null) {
            response.setHeader("Access-Control-Allow-Origin", "*");
            response.setHeader("Cache-Control", "no-cache");
        }
        return erpService.getDeviceLoadRatioByDateInHour(date);
    }

    @RequestMapping(value = "/getDeviceLoadRatioByDateInDay/{date}", method = RequestMethod.GET)
    @Operation(summary = "根据Date获取设备负载率/天", description = "根据Date获取设备负载率/天", method = "GET")
    @Parameter(name = "date", description = "日期", required = true)
    public ResponseVO getDeviceLoadRatioByDateInDay(@PathVariable("date") @DateTimeFormat(pattern = "yyyy-MM-dd") Date date,
                                                    HttpServletResponse response) {
        if (response != null) {
            response.setHeader("Access-Control-Allow-Origin", "*");
            response.setHeader("Cache-Control", "no-cache");
        }
        return erpService.getDeviceLoadRatioByDateInDay(date);
    }

    @RequestMapping(value = "/resource/getLoadRatio/{date}", method = RequestMethod.GET)
    @Operation(summary = "根据Date获取前7天的负载率", description = "根据Date获取前7天的负载率", method = "GET")
    @Parameter(name = "date", description = "日期", required = true)
    public ResponseVO getResourceLoadRatio(@PathVariable("date") @DateTimeFormat(pattern = "yyyy-MM-dd") Date date,
                                                    HttpServletResponse response) {
        if (response != null) {
            response.setHeader("Access-Control-Allow-Origin", "*");
            response.setHeader("Cache-Control", "no-cache");
        }
        return erpService.getResourceLoadRatio(date);
    }

//获取所有产品（包括成品 半成品 原始物料）
    @RequestMapping(value = "/product/getAllItems", method = RequestMethod.GET)
    @Operation(summary = "获取所有产品（包括成品 半成品 原始物料）", description = "获取所有产品（包括成品 半成品 原始物料）", method = "GET")
    public ResponseVO getAllItems(HttpServletResponse response) {
        if (response != null) {
            response.setHeader("Access-Control-Allow-Origin", "*");
            response.setHeader("Cache-Control", "no-cache");
        }
        return erpService.getAllItems();
    }


}
